package handler

import (
	"google.golang.org/protobuf/types/dynamicpb"
	"sen-golang-study/go_game_study/hero_story_go_server/common/log"
	"sen-golang-study/go_game_study/hero_story_go_server/common/server_msg/proto_msg"
)

// CmdHandlerFunc 自定义的消息处理器函数
type CmdHandlerFunc func(ctx CmdContext, pbMsgObj *dynamicpb.Message)

// 指令处理器字典
var cmdHandlerMap = make(map[uint16]CmdHandlerFunc)

// CreateCmdHandler 创建指令处理器
func CreateCmdHandler(msgCode uint16) CmdHandlerFunc {
	return cmdHandlerMap[msgCode]
}

func init() {
	RegisterCmdHandler(proto_msg.MsgCode_USER_LOGIN_CMD, handleCmdUserLogin)
	RegisterCmdHandler(proto_msg.MsgCode_USER_ENTRY_CMD, handleCmdUserEntry)
	RegisterCmdHandler(proto_msg.MsgCode_WHO_ELSE_IS_HERE_CMD, handleWhoElseIsHere)
	RegisterCmdHandler(proto_msg.MsgCode_USER_MOVE_TO_CMD, handleUserMoveTo)
	RegisterCmdHandler(proto_msg.MsgCode_USER_ATTK_CMD, handleUserAttack)
}

func RegisterCmdHandler(msgCode proto_msg.MsgCode, handler CmdHandlerFunc) {
	if handler == nil {
		return
	}

	msgCodeUint := uint16(msgCode.Number())
	exist := cmdHandlerMap[msgCodeUint]
	if exist != nil {
		log.Error("已注册过的指令处理器，指令：%d", msgCode.Number())
		return
	}

	cmdHandlerMap[msgCodeUint] = handler
}
